package taller2.controller.listeners;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import taller2.utils.Constants;
import taller2.view.menus.CodeBrowser;

public class AutoscrollSpeedListener implements ChangeListener{
	private CodeBrowser browser;
	
	// constructor
	public AutoscrollSpeedListener(CodeBrowser browser){
		this.browser = browser;
	}
	
	// detects when user changes the size of the bar
	public void stateChanged(ChangeEvent e) {
		int tam = ((JSlider)e.getSource()).getValue();
		long value = (100-tam)*Constants.ScrollTime;

		if(browser.isAutoscrolling()){		
			browser.stopAutoscroll();	
			browser.setAutoscrollTime(value); 
			browser.startAutoscroll();
		}else{
			browser.setAutoscrollTime(value);
		}
	}
}
